﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Global;

namespace WebHoiChoMuaBan.Controllers.Private
{
    public class HomeController : PrivateController
    {
        //
        // GET: /Home/

        private void Init()
        {
            ViewData["mid"] = "home";
            LoadTimeAndCurrency();
        }

        public ActionResult Index(int? uid, bool isEditMode)
        {
            if (uid == null)
                return HomePage();
            if (LoadMember(uid.Value) == null)
            {
                Init();
                CategoryHelper cateHelper = new CategoryHelper();
                if (isEditMode)
                {
                    ViewData["Mode"] = "Editing";
                    //Lấy tất cả các danh mục của các trang để hiện thị trong combobox
                    //dùng trong trường hợp chỉnh sửa vị trí của danh mục
                    List<Category> list = cateHelper.getCates(0);
                    Category tmpCate = new Category();
                    tmpCate.CateId = -1;
                    tmpCate.CateCount = 0;
                    tmpCate.CateIcon = null;
                    tmpCate.CateName = "Không hiển thị";
                    tmpCate.PageId = null;
                    tmpCate.ParentCateId = null;
                    list.Insert(0, tmpCate);
                    ViewData["ListCates"] = new SelectList(list, "CateId", "CateName");
                    int pageNum;
                    if (!int.TryParse(Request.QueryString["p"], out pageNum))
                        pageNum = 1;
                    NewsProductHelper newsProductHelper = new NewsProductHelper(uid.Value);
                    ViewData["ListNewsProduct"] = newsProductHelper.AllNewProductByMemId(pageNum, Config.ITEMS_PER_PAGE);
                }
                else
                {
                    ViewData["MemId"] = uid;
                    ViewData["Mode"] = "Show";
                    ViewData["isEditMode"] = "False";
                    List<Category> list = cateHelper.getCates(0);
                    Category tmpCate = new Category();
                    tmpCate.CateId = -1;
                    tmpCate.CateCount = 0;
                    tmpCate.CateIcon = null;
                    tmpCate.CateName = "Không hiển thị";
                    tmpCate.PageId = null;
                    tmpCate.ParentCateId = null;
                    list.Insert(0, tmpCate);
                    ViewData["ListCates"] = new SelectList(list, "CateId", "CateName");
                    int pageNum;
                    if (!int.TryParse(Request.QueryString["p"], out pageNum))
                        pageNum = 1;
                    NewsProductHelper newsProductHelper = new NewsProductHelper(uid.Value);
                    //Lay mot CateId bat ky tu CSDL de phan loai danh sach san pham
                    int cateId = cateHelper.getRandomCategory();
                    ViewData["CateId"] = cateId;
                    //ViewData["ListNewsProduct"] = newsProductHelper.AllNewsProductApprovedByMemId(pageNum, Config.ITEMS_PER_PAGE, cateId);
                    ViewData["ListNewsProduct"] = newsProductHelper.AllNewProductByMemId(pageNum, Config.ITEMS_PER_PAGE);
                }
                return View("~/Views/Private/Home.aspx");
            }
            return HomePage();
        }

        [HttpPost]
        public ActionResult Index(FormCollection data, int? uid)
        {
            if (uid == null)
                return HomePage();
            if (LoadMember(uid.Value) == null)
            {
                Init();
                ViewData["Mode"] = "Show";
                CategoryHelper cateHelper = new CategoryHelper();
                List<Category> listLink = new List<Category>();
                //Luu lai cac link danh muc ma user da chon
                MemberHelper memHelper = new MemberHelper();
                Member mem = memHelper.getMember(uid.Value);
                string chainOfId = "";
                for (int i = 1; i <= 12; i++)
                {
                    string idCate = data.Get("drop_" + i);
                    chainOfId += idCate + ",";
                    listLink.Add(cateHelper.getCate(int.Parse(idCate)));
                    ViewData["ListLinks"] = listLink;
                }
                //mem.MemCategoryList = chainOfId;
                //memHelper.Save();
                //int pageNum;
                //if (!int.TryParse(Request.QueryString["p"], out pageNum))
                //    pageNum = 1;
                //NewsProductHelper newsProductHelper = new NewsProductHelper(uid.Value);
                //int cateId = cateHelper.getRandomCategory();
                //ViewData["CurrCateId"] = cateId;
                //ViewData["ListNewsProduct"] = newsProductHelper.AllNewsProductApprovedByMemId(pageNum, Config.ITEMS_PER_PAGE, cateId);
                //ViewData["MemId"] = uid.Value;

                //return View("~/Views/Private/ProductList.aspx");
                return Index(uid,false);
            }
            return HomePage();
        }
    }
}
